;(function($){
	var Route = function( list){
		this.list		= list || {};		//	路由表
		this.status	= 'reload';
		this.module 	= '#home';				//	默认模块
		this.title 	= '';				//	默认标题
		this.status	= 'forward';
		this.timeout	= 3000;				//	默认超时时间
		this.history	= true;				//	是否记录到history
		this.onAfter 	= function(){};
		this.onProgress	= function(){};
		this.onBefore	= function(){};
		this.onTimeout	= function(){};
		this.onSuccess	= function(){};
		this.onError	= function(){};
	}

	Route.prototype = {
		/**
		 * 载入模块
		 * @param {Object} o
		 */
		go: function( o ){
			// //这个位置来存储返回时候的界面
			// if (o.status == "back")
			// {
			// 	sessionStorage.setItem("BACK",o.module)
			// }
			var o			= o || {},
				_s	 		= {};
				
			_s.module 	= typeof o.module != 'undefined'? o.module: this.module;
			_s.title 	= typeof o.title != 'undefined'? o.title: this.title;
			_s.data 	= typeof o.data != 'undefined'? o.data: {};
			_s.timeout 	= !isNaN( o.timeout)? o.timeout: this.timeout;
			_s.history	= typeof o.history != 'undefined'? o.history: this.history;
			_s.status	= typeof o.status != 'undefined'? o.status: this.status;
			_s.onAfter	= typeof o.onAfter == 'function'? o.onAfter: this.onAfter;
			_s.onProgress= typeof o.onProgress == 'function'? o.onProgress: this.onProgress;
			_s.onBefore	= typeof o.onBefore == 'function'? o.onBefore: this.onBefore;
			_s.onTimeout= typeof o.onTimeout == 'function'? o.onTimeout: this.onTimeout;
			_s.onSuccess= typeof o.onSuccess == 'function'? o.onSuccess: this.onSuccess;
			_s.onError	= typeof o.onError == 'function'? o.onError: this.onError;
			var params = _s.module.split("-");
			
			_s.url = this.getModule( params[0] );
			if (params.length > 1)
			{
				var book = params[1].split('&');
				var book_id = sessionStorage.getItem("book_id");
				if (book_id == book[0])
				{
					sessionStorage.setItem("book_id",book[0]);
				}
				else
				{
					sessionStorage.setItem("book_id",book_id);
				}
			}
			if( !_s.url){
				console.error( _s.module+' 模块未定义!');
				return false;
			}
			
			var index = 0;
			var xhr = new XMLHttpRequest();
			xhr.onloadstart = function(){ _s.onAfter();};
			xhr.onloadend = function(e){
				_s.onBefore(e);
			};
			xhr.onprogress = function(e){ _s.onProgress(e);};
			xhr.onreadystatechange = function(e){
				if( xhr.readyState === 4){
					if( xhr.status === 200){
						if(index > 0){
							return;
						}
						index++;
						_s.onSuccess( xhr.responseText);
						if( _s.history){
							var p = _s.module+'?',
								data = _s.data;
							for( var k in data){
								p += k+'='+data[k]+'&';
							}
							p = p.substr( 0, p.length - 1);
							a = location.href.indexOf('#');
							if( a > -1){
							url = window.location.origin+window.location.pathname+p;
								// url = location.href.substring( 0, a)+p;
							}else{
                                url = window.location.origin+window.location.pathname+p
								// url = location.href+"?bid="+sessionStorage.getItem("book_id")+p;
							}
							//history.pushState( data, _s.title+parseInt( ( Math.random() * 100)), url);
                            history.pushState( data, _s.title, url);
						}
					}else{
						_s.onError( xhr.status, xhr.statusText);
					}
				}
			}
			xhr.open('GET', _s.url, false);
			xhr.setRequestHeader("Cache-Control","no-cache");
			xhr.send();
			return params[1] || 0;
		},
		back: function(){
			this.status = 'back';
			history.back();

		},
		forward: function(){
			this.status = 'forward';
			history.forward();
		},
		/**
		 * 获取模块资源位置
		 * @param {Object} m
		 */
		getModule: function( m ){
			// alert(window.location.href);
			if( this.list[m]){
				return this.list[m];
			} else {
			   return this.list['default'];
			}

		},
		/**
		 * 重置模块资源位置
		 * @param {Object} list
		 */
		setModule: function( list){
			for( var k in list){
				this.list[k] = list[k];
			}
		},
		/**
		 * 全局配置
		 * @param {Object} o
		 */
		setup: function(o){
			this.module 	= typeof o.module != 'undefined'? o.module: o.module;
			this.title 		= typeof o.title != 'undefined'? o.title: o.title;
			this.timeout 	= !isNaN( o.timeout)? o.timeout: o.timeout;
			this.onAfter	= typeof o.onAfter == 'function'? o.onAfter: this.onAfter;
			this.onProgress	= typeof o.onProgress == 'function'? o.onProgress: this.onProgress;
			this.onBefore	= typeof o.onBefore == 'function'? o.onBefore: this.onBefore;
			this.onTimeout	= typeof o.onTimeout == 'function'? o.onTimeout: this.onTimeout;
			this.onSuccess	= typeof o.onSuccess == 'function'? o.onSuccess: this.onSuccess;
			this.onError	= typeof o.onError == 'function'? o.onError: this.onError;
		}
	};
	
	$.route = new Route();
	window.addEventListener( 'popstate', function(e){
		var id = $.route.go({
			module: location.hash.split('?')[0] || '#',
			history: false,
			status: 'back'
		});
		
		if (e.state == null)
		{
			localStorage.clear();
		}
		
	});

})(this);
